package base.thread_JUC2.chapter02.demo05;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class PricesInfo {

    private double price1;
    private double price2;
    private ReadWriteLock lock;

    public PricesInfo() {
        price1=1.0;
        price2=2.0;
        lock = new ReentrantReadWriteLock();
    }

    public double getPrice1() {
        lock.readLock().lock();
        double temp = price1;
        lock.readLock().unlock();
        return temp;
    }

    public double getPrice2(){
        lock.readLock().lock();
        double temp = price2;
        lock.readLock().unlock();
        return temp;
    }

    public void setPrices(double price1, double price2) {
        lock.writeLock().lock();
        System.out.printf("--------------开始设置price1: %f、price2: %f \n", price1, price2);
        this.price1 = price1;
        this.price2 = price2;
        lock.writeLock().unlock();
    }

}
